package org.balthie.demo.jdk.util.concurrent.collections.queue;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class DelayQueueDemo
{
    static class MyTask implements Delayed
    {
        String name;
        
        long runningTime;
        
        MyTask(String name, long rt)
        {
            this.name = name;
            this.runningTime = rt;
        }
        
        @Override
        public int compareTo(Delayed other)
        {
            long td = this.getDelay(TimeUnit.MILLISECONDS);
            long od = other.getDelay(TimeUnit.MILLISECONDS);
            return Long.compare(td, od);
        }
        
        @Override
        public long getDelay(TimeUnit unit)
        {
            return unit.convert(runningTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
        }
        
        @Override
        public String toString()
        {
            return name + "-" + runningTime;
        }
    }
}
